﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OSISInstallationDataLayer;
using System.Text;
using System.IO;


namespace OSISInstallationBusinessLayer
{
    /// <summary>
    /// Summary description for InstallationBL 25th June 2009 
    /// </summary>
    /// 

    

    public class InstallationBL
    {
       

        private string language;
        private string dateformat;
        private string timezone;
        private string databasetype;
        private string server;
        private string databasename;
        private string databaseusername;
        private string databasepassword;
        private string port;
        private string adminusername;
        private string adminpassword;
        private string schoolname;
        private string begindate;
        private string enddate;
        private string username;
        private string password;
        private string conn;
        private int schoolyrid;
        private int schoolid;

        private int mingrade;
        private int maxgrade;

        private string gradename;
        


        private string schoolyearname;


        public InstallationBL()
        {
            mingrade = 0;
            maxgrade = 0;
            language = "";
            dateformat = "";
            timezone = "";
            databasetype = "";
            server = "";
            databasename = "";
            databaseusername = "";
            databasepassword = "";
            port = "";
            adminusername = "";
            adminpassword = "";
            schoolname = "";
            begindate = "01/01/1900";
            enddate = "01/01/1900";
            username = "";
            password = "";
            conn = "";
            schoolyrid = 0;
            schoolid = 0;
            Gradename = "";
            schoolyearname = "";

        }


        public string Gradename
        {
            get
            {
                return this.gradename;
            }
            set
            {
                this.gradename = value;
            }
        }

        public String SchoolYearName
        {
            get
            {
                return this.schoolyearname;
            }
            set
            {
                this.schoolyearname = value;
            }
        }



        public int Mingrade
        {
            get
            {
                return this.mingrade;
            }
            set
            {
                this.mingrade = value;
            }
        }
        public int Maxgrade
        {
            get
            {
                return this.maxgrade;
            }
            set
            {
                this.maxgrade = value;
            }
        }


        public int Schoolid
        {
            get
            {
                return this.schoolid;
            }
            set
            {
                this.schoolid = value;
            }
        }




        public int Schoolyrid
        {
            get
            {
                return this.schoolyrid;
            }
            set
            {
                this.schoolyrid = value;
            }
        }

        public String Conn
        {
            get
            {
                return this.conn;
            }
            set
            {
                this.conn = value;
            }
        }
        public String Username
        {
            get
            {
                return this.username;
            }
            set
            {
                this.username = value;
            }
        }
        public String Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
            }
        }
        public string Language
        {
            get
            {
                return this.language;
            }
            set
            {
                this.language = value;
            }
        }
        public string Dateformat
        {
            get
            {
                return this.dateformat;
            }
            set
            {
                this.dateformat = value;
            }
        }

        public string TimeZone
        {
            get
            {
                return this.timezone;
            }
            set
            {
                this.timezone = value;
            }
        }

        public string DatabaseType
        {
            get
            {
                return this.databasetype;
            }
            set
            {
                this.databasetype = value;
            }
        }
        public String Server
        {
            get
            {
                return this.server;
            }
            set
            {
                this.server = value;
            }
        }
        public String Databasename
        {
            get
            {
                return this.databasename;
            }
            set
            {
                this.databasename = value;
            }
        }
        public String DatabaseUsername
        {
            get
            {
                return this.databaseusername;
            }
            set
            {
                this.databaseusername = value;
            }
        }
        public String DatabasePassword
        {
            get
            {
                return this.databasepassword;
            }
            set
            {
                this.databasepassword = value;
            }
        }

        public String Port
        {
            get
            {
                return this.port;
            }
            set
            {
                this.port = value;
            }
        }

        public String AdminUsername
        {
            get
            {
                return this.adminusername;
            }
            set
            {
                this.adminusername = value;
            }
        }
        public String AdminPassword
        {
            get
            {
                return this.adminpassword;
            }
            set
            {
                this.adminpassword = value;
            }
        }
        public String SchoolName
        {
            get
            {
                return this.schoolname;
            }
            set
            {
                this.schoolname = value;
            }
        }

        public String BeginDate
        {
            get
            {
                return this.begindate;
            }
            set
            {
                this.begindate = value;
            }
        }
        public String EndDate
        {
            get
            {
                return this.enddate;
            }
            set
            {
                this.enddate = value;
            }
        }



        public void PopulateTimezone(ref System.Web.UI.WebControls.DropDownList ddl)
        {
            //InstallationDL INDL = new InstallationDL();
            //ddl.Items.Clear();
            ////DataSet ds = null;
            ////ds = INDL.PopulateTimeZone(this);
            ////ddl.DataSource = ds.Tables[0];
            ////ddl.DataTextField = "time_zones";
            ////ddl.DataValueField = "time_zone_id";
            ////ddl.DataBind();
            //ddl.Items.Insert(0, new ListItem("(GMT -12:00) Eniwetok, Kwajalein", "-12"));
        }

        public void PopulateDateFormat(ref System.Web.UI.WebControls.DropDownList ddl)
        {
            InstallationDL INDL = new InstallationDL();
            ddl.Items.Clear();
            //DataSet ds = null;
            //ds = INDL.PopulateDateFormat(this);
            //ddl.DataSource = ds.Tables[0];
            //ddl.DataTextField = "date_format";
            //ddl.DataValueField = "date_format_id";
            //ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("Select", "select"));
            ddl.Items.Insert(1, new ListItem("MM/dd/yyyy", "MM/dd/yyyy"));
            //ddl.Items.Insert(2, new ListItem("dd-MM-yyyy", "dd-MM-yyyy"));
            //ddl.Items.Insert(3, new ListItem("yyyyMMdd", "yyyyMMdd"));
           // ddl.Items.Insert(4, new ListItem("dd/MM/yyyy", "dd/MM/yyyy"));
            ddl.Items.Insert(2, new ListItem("MM-dd-yyyy", "MM-dd-yyyy"));
            ddl.Items.Insert(3, new ListItem("MM-dd-yy", "MM-dd-yy"));
            //ddl.Items.Insert(5, new ListItem("dd-MM-yy", "dd-MM-yy"));
            ddl.Items.Insert(4, new ListItem("MM/dd/yy", "MM/dd/yy"));
            //ddl.Items.Insert(9, new ListItem("dd/MM/yy", "dd/MM/yy"));
            
        }


        public void PopulateLanguages(ref System.Web.UI.WebControls.DropDownList ddl)
        {
            InstallationDL INDL = new InstallationDL();
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("Select", "select"));
            ddl.Items.Insert(1, new ListItem("English", "en"));
            ddl.Items.Insert(2, new ListItem("Spanish", "es"));
            ddl.Items.Insert(3, new ListItem("Chinese", "zh-CHS"));
            ddl.Items.Insert(4, new ListItem("Japanese", "ja"));
            ddl.Items.Insert(5, new ListItem("German", "de"));
            ddl.Items.Insert(6, new ListItem("Korean", "ko"));
            ddl.Items.Insert(7, new ListItem("French", "fr"));
            ddl.Items.Insert(8, new ListItem("Italian", "it"));
            ddl.Items.Insert(9, new ListItem("Russian", "ru"));
            ddl.Items.Insert(10, new ListItem("Portuguese", "pt"));
            ddl.Items.Insert(11, new ListItem("Arabic", "ar"));


        }

        public void PopulateDatabaseType(ref System.Web.UI.WebControls.RadioButtonList rbl)
        {
            InstallationDL INDL = new InstallationDL();
            rbl.Items.Clear();
            
            //DataSet ds = null;
            //ds = INDL.PopulateDatabaseType(this);
            //rbl.DataSource = ds.Tables[0];
            //rbl.DataTextField = "database_type";
            //rbl.DataValueField = "database_type_id";
            //rbl.DataBind();
            rbl.Items.Insert(0, new ListItem("Microsoft SQL Server","msql"));
            rbl.Items.Insert(1, new ListItem("MySQL", "mysql"));
            rbl.Items.Insert(2, new ListItem("Oracle", "Oracle"));
            rbl.Items.Insert(3, new ListItem("DB2", "db2"));
        }

        public void CreateDatabase()
        {
            //InstallationDL INDL = new InstallationDL();
            //INDL.CreateDatabase(this);
        
        }

        public void InsertInstalsettings()
        {
           InstallationDL INDL = new InstallationDL();
           INDL.InsertInstalsettings(this);

        }

        public bool ValidateAdmin()
        {

            InstallationDL INDL = new InstallationDL();
           DataTable dt = INDL.ValidateAdmin(this).Tables[0];
           if (Convert.ToInt32(dt.Rows[0][0]) == 0)
           {
               return false;
           
           }
           else
           {
               return true;
           
           }
        }


        public int AddSchoolName()
        {
            InstallationDL INDL = new InstallationDL();

            int id =INDL.AddSchoolName(this);

            return id;
        
        }


        public int InsertBeginEndDate()
        {
            InstallationDL INDL = new InstallationDL();
           int id = INDL.InsertBeginEnddate(this);
           return id;
        }

        public void InsertBeginEndDateintomarkin()
        {
            InstallationDL INDL = new InstallationDL();
             INDL.InsertintoMarkingPeriod(this);
            
        }


        public int AddGradeLevel()
        {
            InstallationDL INDL = new InstallationDL();
            int id = INDL.AddGradeLevels(this);
            return id;
        }

        public void UpdateMaxGrade()
        {
            InstallationDL INDL = new InstallationDL();
            INDL.UpdateMaxGrade(this);
        }
        public void UpdateMinGrade()
        {
            InstallationDL INDL = new InstallationDL();
            INDL.UpdateMinGrade(this);
        }

        public DataTable getdateformat()
        { 
            InstallationDL INDL = new InstallationDL();
            DataTable dt=INDL.GetDateFormat(this);
            return dt;
        }

        public DataTable getTimeJoneCode()
        {
            InstallationDL INDL = new InstallationDL();
            DataTable dt = INDL.GetTimeJoneCode();
            return dt;
        }



        public int InsertBeginEndDateschooladd()
        {
            InstallationDL INDL = new InstallationDL();
            int id = INDL.InsertBeginEnddateschooladd(this);
            return id;
        }

        public void InsertBeginEndDateintomarkinschooladd()
        {
            InstallationDL INDL = new InstallationDL();
            INDL.InsertintoMarkingPeriodschooladd(this);

        }



        //Mysql Implementation

        public DataTable CheckMysqlDB()
        {
            InstallationDL INDL = new InstallationDL();
            DataTable dt = null;
           dt = INDL.CheckMysqlDB(this);

           return dt;
        
        }

        //added on 22nd september
        public void InsertintoSchoolspeccat()
        {
            InstallationDL INDL = new InstallationDL();
            INDL.InsertintoSchoolspeccat(this);            

        }

        public void InsertintoSchoolspeccatadd()
        {
            InstallationDL INDL = new InstallationDL();
            INDL.InsertintoSchoolspeccatadd(this);

        }


        public void InsertConnectionSettings(string connectionstring, string databasetype)
        {

            //System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath("ConnectionSettings/connectionstring.txt"));
            //StreamWriter1.WriteLine(connectionstring);
            //StreamWriter1.Close();
            //System.IO.StreamWriter StreamWriter2 = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath("ConnectionSettings/databasetype.txt"));
            //StreamWriter1.WriteLine(databasetype);
            //StreamWriter1.Close();

            //InstallationDL INDL = new InstallationDL();




            //system.IO.Path.GetFullPath("");

            //string path = HttpContext.Current.Server.MapPath("../App.config");


         
            string path = System.Web.Hosting.HostingEnvironment.MapPath("/Web.config");

            //string path = System.IO.Path.GetPathRoot("~//App.config");

            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();

            xDoc.Load(path);

            System.Xml.XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings");

            System.Xml.XmlNodeList nodeAppSettings = nodeList[0].ChildNodes;


            System.Xml.XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes;



            xmlAttCollection[0].InnerXml = "sqlConn"; // for key attribute

            xmlAttCollection[1].InnerXml = connectionstring; // for value attribute


            System.Xml.XmlNodeList nodeAppSettingsserv = nodeList[0].ChildNodes;

            System.Xml.XmlAttributeCollection xmlAttCollectionserv = nodeAppSettingsserv[1].Attributes;


            xmlAttCollectionserv[0].InnerXml = "appserver"; // for key attribute

            xmlAttCollectionserv[1].InnerXml = DatabaseType;



            System.Xml.XmlNodeList nodeAppSettingscal = nodeList[0].ChildNodes;

            System.Xml.XmlAttributeCollection xmlAttCollectioncal = nodeAppSettingscal[2].Attributes;


            xmlAttCollectioncal[0].InnerXml = "calendarServer"; // for key attribute

            xmlAttCollectioncal[1].InnerXml = DatabaseType;


            xDoc.Save(path);



            
        }

    }
}
